package courseInfo;


import org.apache.hadoop.io.DoubleWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;

import java.io.IOException;

/*
输入文件为学生成绩信息，包含了必修课与选修课成绩，
格式如下:
班级1，姓名1，科目1，必修，成绩1
班级2，姓名2，科目1，必修，成绩2
班级1，姓名1，科目2，选修，成绩3

实现功能：计算每个学生必修课的平均成绩

*/
public class CourseInfoReducer extends Reducer<Text, DoubleWritable, Text, DoubleWritable> {
    public void reduce(Text key, Iterable<DoubleWritable> values, Context context) throws IOException, InterruptedException{
        int count = 0;
        double grades = 0;
        for(DoubleWritable value : values){
            count ++;
            grades += value.get();
        }

        context.write(key, new DoubleWritable(grades / count));
    }
}
